/*
 * Project: BooEditor
 * Author:  Johannes Kuhaupt
 * Copyright (c): Johannes Kuhaupt
 * Licence: MIT
 */
package BooSyntax;

import org.fife.ui.rsyntaxtextarea.RSyntaxDocument;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.Token;

/**
 *
 * @author Johannes Kuhaupt
 */
public class TabBreakAction extends org.fife.ui.rsyntaxtextarea.RSyntaxTextAreaEditorKit.InsertBreakAction {
    
    int tabSize = 4;
    boolean tabAsSpaces = false;
    
    /**
     * TabBreakAction constructor. Needs two parameters: tab size and the choosen
     * indentation typ (tab or spaces).
     * @param pTabSize
     * @param pTabsAsSpaces
     */
    public TabBreakAction(int pTabSize, boolean pTabsAsSpaces){
        super();
        this.tabSize = pTabSize;
        this.tabAsSpaces = pTabsAsSpaces;
    }
    
    /**
     * TabBreakAction default constructor. Creates a TabBreakAction object with
     * default values: indentation by tab with size = 4
     */
    public TabBreakAction(){
        super();
    }

    /**
     * Handling a line break.
     * @param rsta
     * @param bln
     */
    @Override
    protected void handleInsertBreak(RSyntaxTextArea rsta, boolean bln) {
        super.handleInsertBreak(rsta, bln);
        RSyntaxDocument doc = (RSyntaxDocument)rsta.getDocument();

         int line = rsta.getCaretLineNumber();
         Token t = doc.getTokenListForLine(line-1);
         t = t.getLastNonCommentNonWhitespaceToken();
         
         if(t!=null && t.getLexeme().equalsIgnoreCase(":")){             
             if(tabAsSpaces){
                 String tab = "";
                 for(int i = 0; i<tabSize;i++){
                     tab = tab + " ";
                 }
                 rsta.insert(tab, rsta.getCaretPosition());
             }
             else{
                 rsta.insert("\t", rsta.getCaretPosition());
             }            
         }
        
        
    }
    
    
}
